package main

import (
	"context"
	"fmt"
	"github.com/apache/thrift/lib/go/thrift"
	"net"
	"os"
	"rpc/thrift/t2/rpc"
	"time"
)

var ctx = context.Background()

func main() {
	startTime := currentTimeMillis

	transportFactory := thrift.NewTFramedTransportFactory(thrift.NewTTransportFactory())
	protocolFactory := thrift.NewTBinaryProtocolFactoryDefault()

	transport, err := thrift.NewTSocket(net.JoinHostPort("127.0.0.1", "9000"))
	isErr(err,"err1")
	defer transport.Close()

	err = transport.Open()
	isErr(err,"err2")

	useTransport, err := transportFactory.GetTransport(transport)
	isErr(err,"err3")

	client := rpc.NewRpcDemoClientFactory(useTransport, protocolFactory)
	
	for i := 0; i < 1000; i++ {
		paramMap := make(map[string]string)
		paramMap["name"] = "zhang" + string(i)
		paramMap["passwd"] = "123456"
		r1, e1 := client.FunCall(ctx, currentTimeMillis(), "login", paramMap)
		fmt.Println(i, "call->", r1, e1)
	}

	endTime := currentTimeMillis()

	fmt.Println(endTime, startTime)
}

func currentTimeMillis() int64 {
	return time.Now().UnixNano() / 1000000
}

func isErr(err error,name string){
	if err != nil {
		fmt.Fprintln(os.Stderr, name+":", err)
		os.Exit(1)
	}
}